﻿Imports System
Imports System.IO
Imports System.Data.OleDb
Imports System.Data
Namespace Arwani
    Partial Class Brands
        Inherits System.Web.UI.Page
        '<%# DataBinder.Eval(Container.DataItem, "ViewCount") %>

        Protected Div_Cat As String = ""
        Protected Brand_Logos As String = ""
        Protected Division_Lower_Background As String = ""
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If IsPostBack Then Exit Sub
            If CInt(Request.QueryString("divid")) <> 0 Then
                Call loadData()
                Call loadDivCategories(CInt(Request.QueryString("divid")))
            End If

            Call setDivisionSettings()
        End Sub
        Public Sub setDivisionSettings()
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from division where divisionid=" & CInt(Request.QueryString("divid"))
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Dim iCount As Integer = 0
            If dtTable.Rows.Count > 0 Then
                If Not IsDBNull(dtTable.Rows(0)("backgroundimage")) Then Division_Lower_Background = getDivisionImage(dtTable.Rows(0)("backgroundimage"))
            End If
        End Sub
        Public Sub loadData()
            Dim strQ As String = ""
            If CInt(Request.QueryString("catid")) <> 0 Then
                strQ = "select * from brand b, brand_division_category dbc where b.brandid=dbc.brandid and dbc.divisionid=" & CInt(Request.QueryString("divid")) & " and dbc.categoryid=" & CInt(Request.QueryString("catid")) & " order by dbc.sortorder"
            Else
                strQ = "select * from brand b, brand_division_category dbc where b.brandid=dbc.brandid and dbc.divisionid=" & CInt(Request.QueryString("divid")) & " order by b.sortorder"
            End If

            Dim arwCon As New ArwaniController
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Me.dtBrands.DataSource = dtTable
            Me.dtBrands.DataBind()
            Dim iCount As Integer = 0

            If dtTable.Rows.Count > 0 Then
                Brand_Logos = "<ul id='scroller'>"
                For iCount = 0 To dtTable.Rows.Count - 1
                    Dim _link As String = ""
                    Dim displayOption As Integer = getDisplaOption(CInt(dtTable.Rows(iCount)("brandid")))
                    If displayOption = 1 Then _link = "Product_Signle.aspx?bid=" & CInt(dtTable.Rows(iCount)("brandid")) & "&divid=" & CInt(Request.QueryString("divid"))
                    If displayOption = 2 Then _link = "Product_Double.aspx?bid=" & CInt(dtTable.Rows(iCount)("brandid")) & "&divid=" & CInt(Request.QueryString("divid"))
                    If displayOption = 4 Then _link = "Product_4.aspx?bid=" & CInt(dtTable.Rows(iCount)("brandid")) & "&divid=" & CInt(Request.QueryString("divid"))
                    Brand_Logos &= "<li><div class='thumb'><a href='" & _link & "'><img src='" & getImage(dtTable.Rows(iCount)("brandlogo")) & "' width='196' height='67'/></a></div></li>"
                Next
                Brand_Logos &= "</ul>"
            End If

        End Sub

        Public Sub loadDivCategories(ByVal divid As Integer)

            Dim arwCon As New ArwaniController
            Dim strQ As String = "select distinct catname,catid  from brand_division_category bdc, category c where c.catid=bdc.categoryid and  bdc.divisionid=" & divid & ""
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            Dim iCount As Integer = 0
            Div_Cat = "<ul>"
            Div_Cat &= "<li class='active'><a href='Brands.aspx?divid=" & CInt(Request.QueryString("divid")) & "'>All</a></li>"
            If dtTable.Rows.Count > 0 Then
                For iCount = 0 To dtTable.Rows.Count - 1
                    Div_Cat &= "<li><a href='Brands.aspx?divid=" & CInt(Request.QueryString("divid")) & "&catid=" & dtTable.Rows(iCount)("catid") & "'>" & dtTable.Rows(iCount)("catname") & "</a></li>"
                    ' Brand_Logos &= "<li><div class='thumb'><img src='images/brand_logo1.png' width='196' height='67'  /></div></li>"
                Next
            End If
            Div_Cat &= "</ul>"

        End Sub



        Public Function getDivisionImage(ByVal imgSrc As String) As String
            Dim strReturn As String = "images/division_bg.png"
            If File.Exists(Server.MapPath("Divisions/" & imgSrc)) Then
                strReturn = "Divisions/" & imgSrc
            End If
            Return strReturn
        End Function
        Public Function getImage(ByVal imgSrc As String) As String
            Dim strReturn As String = "images/main_banner.png"
            If File.Exists(Server.MapPath("Brands/" & "Thumb_" & imgSrc)) Then
                strReturn = "Brands/" & "Thumb_" & imgSrc
            End If
            Return strReturn
        End Function
        Protected Sub dtBrands_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles dtBrands.ItemCommand
            'Dim imgButton As ImageButton = CType(e.Item.FindControl("imgButton"), ImageButton)
            Dim displayOption As Integer = getDisplaOption(CInt(e.CommandArgument))
            If displayOption = 1 Then Response.Redirect("Product_Signle.aspx?bid=" & CInt(e.CommandArgument) & "&divid=" & CInt(Request.QueryString("divid")))
            If displayOption = 2 Then Response.Redirect("Product_Double.aspx?bid=" & CInt(e.CommandArgument) & "&divid=" & CInt(Request.QueryString("divid")))
            If displayOption = 4 Then Response.Redirect("Product_4.aspx?bid=" & CInt(e.CommandArgument) & "&divid=" & CInt(Request.QueryString("divid")))
        End Sub

        Protected Sub dtBrands_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dtBrands.ItemDataBound
            Try

                Dim imgButton As ImageButton = CType(e.Item.FindControl("imgButton"), ImageButton)
            Catch ex As Exception
            End Try
        End Sub

        Public Function getDisplaOption(ByVal BrandID As Integer) As String
            Dim arwCon As New ArwaniController
            Dim strQ As String = "select * from brand where brandid=" & CInt(BrandID)
            Dim dtTable As DataTable = arwCon.ExecuteQuery(strQ)
            
            If dtTable.Rows.Count > 0 Then
                If dtTable.Rows(0)("displayoption").ToString() <> "" Then
                    Return CInt(dtTable.Rows(0)("displayoption").ToString())
                End If

            End If
            Return 1
        End Function

    End Class
End Namespace
